﻿using AnesSystem.Models;
using AnesSystem.Services;
using Common;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    class ConfigDrugGroupTypesCtrlViewModel : BaseViewModel<ConfigDrugGroupTypesCtrlViewModel>
    {
        #region Variables

        private string _seachValue = "";

        private ObservableCollection<ConfigDrugGroupTypesModel> _displayConfigDrugGroupTypesModels = new ObservableCollection<ConfigDrugGroupTypesModel>();

        #endregion

        #region Constructor

        public ConfigDrugGroupTypesCtrlViewModel()
        {
        }



        #endregion

        #region Properties
        public ObservableCollection<ConfigDrugGroupTypesModel> DisplayConfigDrugGroupTypesModels
        {
            get
            {

                _displayConfigDrugGroupTypesModels.Clear();
                List<ConfigDrugGroupTypesModel> lst = ConfigDrugGroupTypesDataGridViewModel.Instance.ConfigDrugGroupTypesModel.ToList().Where(O =>O.Name.Contains(_seachValue) || O.Code.Contains(_seachValue)).ToList();
                foreach (var item in lst)
                {
                    item.PropertyChanged += Item_PropertyChanged;
                    _displayConfigDrugGroupTypesModels.Add(item);
                }
                return _displayConfigDrugGroupTypesModels;
            }

            set
            {
                ConfigDrugGroupTypesDataGridViewModel.Instance.ConfigDrugGroupTypesModel = value;
                RaisePropertyChanged("DisplayConfigDrugGroupTypesModels");
            }
        }
        public string SeachValue
        {
            get
            {
                return _seachValue;
            }

            set
            {
                _seachValue = value;
                RaisePropertyChanged("DisplayConfigDrugGroupTypesModels");
            }
        }


        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigDrugGroupTypesModelService.Instance.Update(sender as ConfigDrugGroupTypesModel);
        }
        private void AddConfigDrugGroupTypesModel(ConfigDrugGroupTypesModel obj)
        {
            if (obj.Id != new ObjectId("000000000000000000000000"))
            {

                ConfigDrugGroupTypesModelService.Instance.Update(obj);
                ConfigDrugGroupTypesDataGridViewModel.Instance.ConfigDrugGroupTypesModel.Add(obj);
            }
            else ConfigDrugGroupTypesModelService.Instance.Insert(obj);


        }

        #endregion

        #region Public/Protected Methods
        public void DelConfigDrugGroupTypesModel(ConfigDrugGroupTypesModel configDrugGroupTypesModel)
        {
            if (configDrugGroupTypesModel != null)
            {
                ConfigDrugGroupTypesDataGridViewModel.Instance.ConfigDrugGroupTypesModel.Remove(configDrugGroupTypesModel);
                RaisePropertyChanged("DisplayConfigDrugGroupTypesModels");
                ConfigDrugGroupTypesModelService.Instance.DeleteById(configDrugGroupTypesModel.Id);
            }
        }

        public void AddRow()
        {
            ConfigDrugGroupTypesModel model = new ConfigDrugGroupTypesModel();
            DisplayConfigDrugGroupTypesModels.Insert(0, model);
            model.PropertyChanged += Model_PropertyChanged;
        }
        #endregion

        #region EventHandlers
        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            AddConfigDrugGroupTypesModel(sender as ConfigDrugGroupTypesModel);
        }

        #endregion

    
        #region Command

        #region ChangeDoctorCommand

        #endregion

        #region OpenPopupCommand

        #endregion
        #endregion
    }
}
